package org.example.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import dto.StandardQuaryDTO;

import entity.*;
import jakarta.annotation.Resource;
import org.example.constant.JwtClaimsConstant;
import org.example.constant.StandardConstant;
import org.example.exception.BaseException;
import org.example.repository.ProcessRepository;
import org.example.result.PageResult;
import org.example.service.ProcessService;
import org.example.utils.ThreadLocalUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class ProcessServiceImpl implements ProcessService {

    @Resource
    ProcessRepository processRepository;

    /**
     * 查询原材料标准
     * @param standardQuaryDTO
     * @return
     */
    @Override
    public PageResult queryRawMaterialStandards(StandardQuaryDTO standardQuaryDTO) {
        standardQuaryDTO.setStandardType(StandardConstant.RAW_MATERIAL_STANDARD_TYPE);
        // 开始分页查询
        PageHelper.startPage(standardQuaryDTO.getPageNum(), standardQuaryDTO.getPageSize());
        Page<QualityStandard> qualityStandard = processRepository.queryQualityStandard(standardQuaryDTO);
        return new PageResult(qualityStandard.getTotal(), qualityStandard.getResult());
    }
    /**
     * 查询半成品标准
     * @param standardQuaryDTO
     * @return
     */
    @Override
    public PageResult querySemiFinishedStandards(StandardQuaryDTO standardQuaryDTO) {
        standardQuaryDTO.setStandardType(StandardConstant.SEMI_FINISHED_STANDARD_TYPE);
        // 开始分页查询
        PageHelper.startPage(standardQuaryDTO.getPageNum(), standardQuaryDTO.getPageSize());
        Page<QualityStandard> qualityStandard = processRepository.queryQualityStandard(standardQuaryDTO);
        return new PageResult(qualityStandard.getTotal(), qualityStandard.getResult());
    }
    /**
     * 查询成品标准
     * @param standardQuaryDTO
     * @return
     */
    @Override
    public PageResult queryFinishedStandards(StandardQuaryDTO standardQuaryDTO) {
        standardQuaryDTO.setStandardType(StandardConstant.FINISHED_STANDARD_TYPE);
        // 开始分页查询
        PageHelper.startPage(standardQuaryDTO.getPageNum(), standardQuaryDTO.getPageSize());
        Page<QualityStandard> qualityStandard = processRepository.queryQualityStandard(standardQuaryDTO);
        return new PageResult(qualityStandard.getTotal(), qualityStandard.getResult());
    }


    @Override
    public void addStandard(QualityStandard qualityStandard) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get(JwtClaimsConstant.USER_ID);
        qualityStandard.setStandardStatus(StandardConstant.STANDARD_STATUS_DISABLE);
        qualityStandard.setCreatorId(userId);
        qualityStandard.setCreateTime(LocalDateTime.now());
        qualityStandard.setUpdateTime(LocalDateTime.now());
        processRepository.addStandard(qualityStandard);
    }

    @Override
    public void updateStandard(QualityStandard  qualityStandard) {
        qualityStandard.setUpdateTime(LocalDateTime.now());
        processRepository.updateStandard(qualityStandard);
    }

    @Override
    public Boolean deleteStandard(Integer id, String standardStatus) {
        if (StandardConstant.STANDARD_STATUS_ENABLE.equals(standardStatus)){
            return false;
        }
        processRepository.deleteStandard(id);
        return true;
    }

    /**
     * 更新标准状态
     * @param qualityStandard
     */
    @Override
    public void updateStandardStatus(QualityStandard qualityStandard) {
        if (StandardConstant.STANDARD_STATUS_ENABLE.equals(qualityStandard.getStandardStatus())){
            qualityStandard.setStandardStatus(StandardConstant.STANDARD_STATUS_DISABLE);
        }else {
            qualityStandard.setStandardStatus(StandardConstant.STANDARD_STATUS_ENABLE);
        }
        qualityStandard.setUpdateTime(LocalDateTime.now());
        processRepository.updateStandard(qualityStandard);
    }


}
